#ifndef HELMETTIMERS_H
#define HELMETTIMERS_H

#include "HelmetSystemConfig.h"
#include "HelmetLEDs.h"
#include "HelmetInputs.h"

/**************************************************************************************
	Global Variables
**************************************************************************************/
unsigned short int SecondParts;
unsigned int LogHangTime = 0; // Not logging hang-time

// Main loop control
unsigned int MainReportHangTime = 0; // Do not report hang-time value

/**************************************************************************************
	Definitions
**************************************************************************************/
#define T1_FREQ 100 // frequency of T1 interrupt

// Macro to instruct micro that a 'new second' has started
// a second has 2 fewer parts than the number of T1 interrupts
// per second to reduce any discrepancies between 1 second as
// defined by the GPS and 1 second as defined by the PIC
#define startNewSecond() SecondParts=T1_FREQ-2;

// One 'second part' has gone by, decrement the count.
// When the count reaches zero (i.e. - has no parts left), 
// the second is old and should be decremented no more.
#define secondPartElapsed() if(SecondParts>0) SecondParts--;

// Macro to test whether second is 'old'
// i.e. - that it has not parts left
#define isOldSecond() ( SecondParts==0 ? 1 : 0 )

// Note on second parts:
//       The first new character sent by the GPS
//       received by the PIC begins a 'new second'.
//       The timer sets the second as 'old' just 
//       before the new second is expected to start
//       long after the GPS will have sent its data.
//       See HelmetUART.h's GPShandler routine for
//       an example.

/**************************************************************************************
	Function Prototypes
**************************************************************************************/
void ConfigTimer1( void );

/**************************************************************************************
	Functions
**************************************************************************************/
void ConfigTimer1() {
	#define T1_TICK (( GetPeripheralClock() / 256 / T1_FREQ ))
	OpenTimer1( T1_ON | T1_SOURCE_INT | T1_PS_1_256, T1_TICK );
    ConfigIntTimer1( T1_INT_ON | T1_INT_PRIOR_3 );
}

/**************************************************************************************
	Interrupt handling routines
**************************************************************************************/
// Note: Priority 1 (lowest) to 7 (highest)

/**************************************
		Timer 1
			Priority 3
**************************************/
void __ISR(_TIMER_1_VECTOR, ipl3) Timer1Handler(void)
{    
    // clear the interrupt flag
	INTClearFlag( INT_T1 );
	
    ToggleHEARTBEAT();
    ClearGPSLED();
    ClearAtomLED();
    
    // one second part has elapsed
    secondPartElapsed();
    
    // If in free fall, increment hang time counter
    if( Read0GD() ) {
	    LogHangTime++;
	}
	// If not in free fall and hang time has been counted,
	// tell main to report the hang time
	else if( LogHangTime > 0 ) {
		MainReportHangTime=1; // report hang time
	}
	else {
		/* End World Hunger */
	}		
    
    // Make sure the flag is cleared
    INTClearFlag( INT_T1 );
}

#endif